package mof.gov.dmdc.view.common;

import java.util.HashMap;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import oracle.jbo.domain.Date;

public class TextConverter {
    public static final String utf8String =
        "A;a;á;á;à;à;ả;ả;ã;ã;ạ;ạ;Â;â;ấ;ấ;ầ;ầ;ẩ;ẩ;ẫ;ẫ;ậ;ậ;Ă;ă;ắ;ắ;ằ;ằ;ẳ;ẳ;ẵ;ẵ;ặ;ặ;B;b;C;c;D;d;Đ;đ;E;e;é;é;è;è;ẻ;ẻ;ẽ;ẽ;ẹ;ẹ;Ê;ê;ế;ế;ề;ề;ể;ể;ễ;ễ;ệ;ệ;F;f;G;g;H;h;I;i;í;í;ì;ì;ỉ;ỉ;ĩ;ĩ;ị;ị;J;j;K;k;L;l;M;m;N;n;O;o;ó;ó;ò;ò;ỏ;ỏ;õ;õ;ọ;ọ;Ô;ô;ố;ố;ồ;ồ;ổ;ổ;ỗ;ỗ;ộ;ộ;Ơ;ơ;ớ;ớ;ờ;ờ;ở;ở;ỡ;ỡ;ợ;ợ;P;p;Q;q;R;r;S;s;T;t;U;u;ú;ú;ù;ù;ủ;ủ;ũ;ũ;ụ;ụ;Ư;ư;ứ;ứ;ừ;ừ;ử;ử;ữ;ữ;ự;ự;V;v;W;w;X;x;Y;y;ý;ý;ỳ;ỳ;ỷ;ỷ;ỹ;ỹ;ỵ;ỵ;Z;z";
    
    public static final String tcvn3String =
        "A;a;¸;¸;µ;µ;¶;¶;·;·;¹;¹;¢;©;Ê;Ê;Ç;Ç;È;È;É;É;Ë;Ë;¡;¨;¾;¾;»;»;¼;¼;½;½;Æ;Æ;B;b;C;c;D;d;§;®;E;e;Ð;Ð;Ì;Ì;Î;Î;Ï;Ï;Ñ;Ñ;£;ª;Õ;Õ;Ò;Ò;Ó;Ó;Ô;Ô;Ö;Ö;F;f;G;g;H;h;I;i;Ý;Ý;×;×;Ø;Ø;Ü;Ü;Þ;Þ;J;j;K;k;L;l;M;m;N;n;O;o;ã;ã;ß;ß;á;á;â;â;ä;ä;¤;«;è;è;å;å;æ;æ;ç;ç;é;é;¥;¬;í;í;ê;ê;ë;ë;ì;ì;î;î;P;p;Q;q;R;r;S;s;T;t;U;u;ó;ó;ï;ï;ñ;ñ;ò;ò;ô;ô;¦;­;ø;ø;õ;õ;ö;ö;÷;÷;ù;ù;V;v;W;w;X;x;Y;y;ý;ý;ú;ú;û;û;ü;ü;þ;þ;Z;z";

    private String[] utf8;
    private String[] tcvn3;

    private HashMap Utf8Tcvn3Map;

    public TextConverter() {
        System.out.println("A;a;\u00B8;\u00B8;\u00B5;\u00B5;\u00B6;\u00B6;\u00B7;\u00B7;\u00B9;\u00B9;\u00A2;\u00A9;\u00CA;\u00CA;\u00C7;\u00C7;\u00C8;\u00C8;\u00C9;\u00C9;\u00CB;\u00CB;\u00A1;\u00A8;\u00BE;\u00BE;\u00BB;\u00BB;\u00BC;\u00BC;\u00BD;\u00BD;\u00C6;\u00C6;B;b;C;c;D;d;\u00A7;\u00AE;E;e;\u00D0;\u00D0;\u00CC;\u00CC;\u00CE;\u00CE;\u00CF;\u00CF;\u00D1;\u00D1;\u00A3;\u00AA;\u00D5;\u00D5;\u00D2;\u00D2;\u00D3;\u00D3;\u00D4;\u00D4;\u00D6;\u00D6;F;f;G;g;H;h;I;i;\u00DD;\u00DD;\u00D7;\u00D7;\u00D8;\u00D8;\u00DC;\u00DC;\u00DE;\u00DE;J;j;K;k;L;l;M;m;N;n;O;o;\u00E3;\u00E3;\u00DF;\u00DF;\u00E1;\u00E1;\u00E2;\u00E2;\u00E4;\u00E4;\u00A4;\u00AB;\u00E8;\u00E8;\u00E5;\u00E5;\u00E6;\u00E6;\u00E7;\u00E7;\u00E9;\u00E9;\u00A5;\u00AC;\u00ED;\u00ED;\u00EA;\u00EA;\u00EB;\u00EB;\u00EC;\u00EC;\u00EE;\u00EE;P;p;Q;q;R;r;S;s;T;t;U;u;\u00F3;\u00F3;\u00EF;\u00EF;\u00F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B0;\u1EE9;\u1EE9;\u1EEB;\u1EEB;\u1EED;\u1EED;\u1EEF;\u1EEF;\u1EF1;\u1EF1;V;v;W;w;X;x;Y;y;\u00FD;\u00FD;\u1EF3;\u1EF3;\u1EF7;\u1EF7;\u1EF9;\u1EF9;\u1EF5;\u1EF5;Z;z");
        System.out.println(tcvn3String);
        System.out.println(utf8String);
        tcvn3 = this.CSVStringToStringArray(tcvn3String, ";");
        utf8 = this.CSVStringToStringArray(utf8String, ";");
        Utf8Tcvn3Map = new HashMap();
        int i = 0;
        for (i = 0; i < utf8.length - 1; i++) {
            Utf8Tcvn3Map.put(utf8[i], tcvn3[i]);
        }
    }

    public String convertUtf8ToTcvn3(String utf8Str) {
        String tmpStr = "";
        String tmp = "";
        String outStr = "";
        for (int i = 0; i < utf8Str.length(); i++) {
            tmpStr = utf8Str.substring(i, i + 1);
            if (utf8String.indexOf(tmpStr) >= 0) {
                try {
                    tmp = Utf8Tcvn3Map.get(tmpStr).toString();
                } catch (Exception npe) {
                    tmp = tmpStr;
                }
                outStr = outStr + tmp;
            } else {
                outStr = outStr + tmpStr;
            }
        }
        return outStr;
    }

    private String[] CSVStringToStringArray(String aString, String delimText) {
        String[] splittArray = null;
        if (aString != null || !aString.equalsIgnoreCase("")) {
            splittArray = aString.split(delimText);
        }
        return splittArray;
    }

    public String convertDate2String(Date dObj) {
        java.util.Date dtTmp = dObj.getValue();

        Format formatter = new SimpleDateFormat("dd/MM/yyyy");
        String s = formatter.format(dtTmp);
        return s;
    }

    public String convertNumber2String(Double num) {
        NumberFormat formatter = new DecimalFormat("#0.00");
        String s = formatter.format(num);
        return s;
    }
}
